package zcw.com.basic.exception;

/**
 * Created by 朱城委 on 2019/7/17.<br><br>
 */
public class TestException {

    public TestException() {
    }

    boolean testEx() throws Exception {
        boolean ret = true;

        try {
            ret = testEx1();
        }
        catch (Exception e) {
            System.out.println("testEx, catch exception");
            ret = false;
            throw e;
        }
        finally {
            System.out.println("testEx, finally; return value = " + ret);
            return ret;
        }
    }

    boolean testEx1() throws Exception {
        boolean ret = true;

        try {
            ret = testEx2();
            if(!ret) {
                return false;
            }

            System.out.println("testEx1, at the end of try");
            return ret;
        }
        catch (Exception e) {
            System.out.println("testEx1, catch exception");
            ret = false;
            throw e;
        }
        finally {
            System.out.println("testEx1, finally; return value = " + ret);
            return ret;
        }
    }

    boolean testEx2() throws Exception {
        boolean ret = true;

        try {
            int b = 12;
            int c;
            for(int i = 2; i >= -2; i--) {
                c = b / i;
                System.out.println("i = " + i);
            }

            return true;
        }
        catch (Exception e) {
            System.out.println("testEx2, catch exception");
            ret = false;
            throw e;
        }
        finally {
            System.out.println("testEx2, finally; return value = " + ret);
            return ret;
        }
    }

    public static final String test() {
        String t = "";

        try {
            t = "try";
            return t;
        } catch (Exception e) {
            // result = "catch";
            t = "catch";
            return t;
        } finally {
            t = "finally";
//            return t;
        }
    }

    public static void test2() {
        try {
            int a = 10 / 0;
        }
//        catch (Exception e) {
//            throw e;
//        }
        finally {
            System.out.println("finally");
            return ;
        }
    }

    public static void main(String[] args) {
        TestException testException = new TestException();
        try {
            testException.testEx();
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(test());
        test2();
    }
}
